package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.Constant;
import com.chen.dao.mapper.CoachMapper;
import com.chen.dao.pojo.Coach;
import com.chen.service.CoachService;
import com.chen.vo.CoachVo;
import com.chen.vo.Result;
import com.chen.vo.SelectVo;
import com.chen.vo.param.CoachParam;
import java.util.LinkedList;
import java.util.List;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:chenao
 * @DATE:2022/3/13 16:24
 */
@Service
public class CoachServiceImpl implements CoachService {

    @Autowired
    private CoachMapper coachMapper;

    @Override
    public Result saveCoach(CoachParam param) {
        Coach coach = new Coach();coach.setCoachAuth(param.getCoachAuth());
        coach.setAge(param.getAge());
        coach.setGender(param.getGender());
        coach.setTeachAge(param.getTeachAge());
        coach.setName(param.getName());
        coach.setUsername(param.getUsername());
        coach.setPassword(param.getPassword());
        coach.setIntroduce(param.getIntroduce());
        if (Strings.isNotBlank(param.getId())){

            coach.setId(param.getId());
            coachMapper.updateById(coach);
            return Result.success("ok");
        }
        //添加的时候设置默认头像
        if (param.getGender().equals("0")){
            coach.setAvatar(Constant.COACH_AVATAR_MALE.getvalue());
        }else{
            coach.setAvatar(Constant.COACH_AVATAR_FEMALE.getvalue());
        }
        coach.setRateNum(0);
        coach.setGrade(0.0);
        coachMapper.insert(coach);
        return Result.success("ok");
    }

    @Override
    public Result listCoach(CoachParam param) {
        Page<Coach> page = new Page<>(param.getPage(),param.getLimit());

        LambdaQueryWrapper<Coach> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Coach::getId, param.getId());
        wrapper.like(Coach::getName,param.getName());

        coachMapper.selectPage(page,wrapper);
        List<CoachVo> coachVoList = new LinkedList<>();
        page.getRecords().stream().forEach(item->{
            CoachVo coachVo = new CoachVo();
            BeanUtils.copyProperties(item,coachVo);
            if (coachVo.getGender().equals("0")){
                coachVo.setGender("男");
            } else {
                coachVo.setGender("女");
            }
            coachVoList.add(coachVo);
        });
        return Result.success(coachVoList,page.getTotal());
    }

    @Override
    public Result listCoachName() {
        LambdaQueryWrapper<Coach> wrapper = new LambdaQueryWrapper<>();
        List<Coach> coachList= coachMapper.selectList(null);

        LinkedList<SelectVo> selectVoList = new LinkedList<>();
        coachList.stream().forEach(item->{
            SelectVo selectVo = new SelectVo();
            selectVo.setKey(item.getName());
            selectVo.setValue(item.getId());
            selectVoList.add(selectVo);
        });
        return Result.success(selectVoList);
    }

    @Override
    public Result deleteCoachById(String id) {
        int result = coachMapper.deleteById(id);
        return Result.success(result);
    }

}
